<template>
  <div class="about">
    <h1>{{ id ? "编辑" : "新建" }}汽车</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <!-- 获取品牌分类 -->
      <el-tabs type="border-card" value="basic">
        <el-tab-pane label="基本信息" name="basic">
          <el-form-item label="品牌">
            <el-select v-model="model.brand">
              <el-option
                v-for="brand_item in brands"
                :key="brand_item._id"
                :label="brand_item.name"
                :value="brand_item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="颜色">
            <el-select v-model="model.color">
              <el-option
                v-for="color_item in color"
                :key="color_item._id"
                :label="color_item.name"
                :value="color_item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="车型">
            <el-select v-model="model.model">
              <el-option
                v-for="carModel_item in carModel"
                :key="carModel_item._id"
                :label="carModel_item.name"
                :value="carModel_item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="燃料类型">
            <el-select v-model="model.fuelType">
              <el-option
                v-for="fuelType_item in fuelType"
                :key="fuelType_item._id"
                :label="fuelType_item.name"
                :value="fuelType_item._id"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="车名">
            <el-input v-model="model.name"></el-input>
          </el-form-item>
          <el-form-item label="图片">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="afterUpload"
              
            >
            <!-- :before-upload="beforeAvatarUpload" -->
              <img v-if="model.icon" :src="model.icon" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="车龄">
            <el-input v-model="model.age"></el-input>
          </el-form-item>
          <el-form-item label="里程">
            <el-input v-model="model.milage"></el-input>
          </el-form-item>
          <el-form-item label="价格">
            <el-input v-model="model.price"></el-input>
          </el-form-item>
          <el-form-item label="变速器类型">
            <el-input v-model="model.TransmissionType"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="详细图片" name="detailPics">
          <el-button  size="small" @click="model.detailPics.push({})">
            <i class="el-icon-plusin"></i>添加信息
            </el-button>
          <el-row type="flex" style="flex-wrap:wrap">
            <el-col :md="12" v-for="(item,i) in model.detailPics" :key="i">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图片">
               <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="res=>$set(item,'icon',res.url)"
             
            >
              <!-- :before-upload="beforeAvatarUpload" -->
              <img v-if="item.icon" :src="item.icon" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button size="small" type="danger"
                 @click="model.detailPics.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item style="margin-top:1rem;">
        <el-button type="primary" native-type="submit">保存 </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        detailPics:[]
      },
      brands: [],
      color: [],
      fuelType: [],
      carModel: [],
    };
  },

  methods: {
    afterUpload(res) {
      this.$set(this.model, "icon", res.url);
      // this.model.icon=res.url//普通语法，赋值赋不上
    },
    async save() {
      //新建是post，编辑put
      let res;
      if (this.id) {
        //如果有id传递到有id的接口，去修改
        res = await this.$http.put(`rest/cars/${this.id}`, this.model);
      } else {
        //没有id则传递到没有id的接口，去新建
        res = await this.$http.post("rest/cars", this.model);
      }
      console.log(res);
      this.$router.push("/cars/list");
      this.$message({
        type: "success",
        message: "保存成功",
      });
    },

    async fetch() {
      const res = await this.$http.get(`rest/cars/${this.id}`);
      this.model = res.data;
    },

    async fetchBrands() {
      const res = await this.$http.get(`category/brand`);
      this.brands = res.data;
    },

    async fetchCarModel() {
      const res = await this.$http.get(`category/carModel`);
      this.carModel = res.data;
    },

    async fetchColor() {
      const res = await this.$http.get(`category/color`);
      this.color = res.data;
    },
    //获取燃料类型
    async fetchFuelType() {
      const res = await this.$http.get(`category/fuelType`);
      this.fuelType = res.data;
    },
  },

  created() {
    this.fetchBrands();
    this.fetchCarModel();
    this.fetchColor();
    this.fetchFuelType();
    this.id && this.fetch(); //有需要编辑的id才执行方法
  },
};
</script>

